<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      li {
        list-style: none;
      }
      .dialog {
        width: 80%;
        height: 70vh;
        min-width: 400px;
        margin: 50px auto;
        border: 2px solid skyblue;
        border-top-left-radius: 20px;
        border-top-right-radius: 20px;
        overflow: auto;
      }
      .send {
        position: fixed;
        left: 50%;
        display: flex;
        bottom: 30px;
        width: 80%;
        height: 40px;
        transform: translateX(-50%);
      }
      .send input {
        flex: 1;
        outline: none;
        border: 0;
        background-color: #ddd;
        padding: 0 20px;
      }
      .send button,
      .send select {
        width: 100px;
        height: 100%;
        background-color: red;
        color: #fff;
        margin-left: 2px;
        border: 0;
      }
      /* 定制滚动条 */
      .dialog::-webkit-scrollbar {
        width: 0px;
      }
      ul {
        display: flex;
        flex-direction: column;
      }
      li {
        position: relative;
        margin-top: 50px;
        padding: 0 70px;
      }
      li span {
        position: absolute;
        top: 0;
        right: 10px;
      }
      .right {
        text-align: right;
      }
      .left span {
        left: 10px;
      }
      ul li span {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background-color: skyblue;
        text-align: center;
        line-height: 50px;
        overflow: hidden;
        top: -15px;
        font-size: 12px;
      }
      button:active {
        cursor: pointer;
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div class="dialog">
      <ul id="ul"></ul>
    </div>
    <div class="send">
      <input type="text" id="ipt" autocomplete="off" placeholder="说点什么" />
      <button>send</button>
      <select name="" id="select"></select>
    </div>
    <script src="./js/soket.js"></script>
    <script>
      // scoket服务器连接
      const socket = io(
        `ws://localhost:3000?token=${localStorage.getItem("token")}`
      );
      //定义接受后端类型
      const msgType = {
        chatError: 0,
        chatList: 1,
        chatGroup: 2,
        singleChat: 3,
      };
      // --------------------------------------发送消息
      // 进来直接去请求列表
      socket.emit(msgType.chatList);
      document.querySelector("button").addEventListener("click", function () {
        // 如果为空return
        if (!ipt.value.trim()) return alert("不能为空");
        if (select.value === "all") {
          // 发送群聊
          socket.emit(msgType.chatGroup, chatData(ipt.value));
          // 因为群聊没有给自己发所以要自己渲染页面
          renderLi(chatData(ipt.value, localStorage.getItem("username")), true);
        } else {
          // 私聊 发送想要聊天的用户给客户端和数据
          socket.emit(msgType.singleChat, chatData(ipt.value, select.value));
          // 因为群聊没有给自己发所以要自己渲染页面
          renderLi(chatData(ipt.value, localStorage.getItem("username")), true);
        }
        //清空聊天框
        ipt.value = "";
      });

      // ---------------------------------------接受消息
      // 接受群聊消息
      socket.on(msgType.chatGroup, (msg) => {
        renderLi(msg);
      });
      // 接收列表信息
      socket.on(msgType.chatList, (msg) => {
        select.innerHTML =
          `  <option value="all">all</option>` +
          msg
            .map(
              (item) => `
        <option value="${item}">${item}</option>
        `
            )
            .join("");
      });
      // 接受错误消息
      socket.on(msgType.chatError, (msg) => {
        localStorage.removeItem("token");
        location.href = "/login";
      });
      // 接受私聊消息
      socket.on(msgType.singleChat, (msg) => {
        renderLi(msg);
      });
      // ------------------------------------------方法
      // 定义渲染ul中li
      function renderLi(data, flag) {
        // 创建一个小 li
        const li = document.createElement("li");
        //  给li 加class类  判断 flag 来决定左右
        li.className = flag ? "right" : "left";
        // 把数据放进去
        li.innerHTML = `<span>${data.user}</span>${data.data}`;
        ul.appendChild(li);
        // 聊天框到最低下
        scrollTo();
      }
      // 定义接受后端的数据
      function chatData(data, user) {
        return {
          data,
          user,
        };
      }
      // 聊天框滚动事件
      function scrollTo() {
        document.querySelector(".dialog").scrollTo(0, ul.offsetHeight);
      }
    </script>
  </body>
</html>
